```python
from langchain.chains import RetrievalQA
from langchain.document_loaders import TextLoader
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.llms import OpenAI
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import Chroma
```


```python
loader = TextLoader("../../state_of_the_union.txt")
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(documents)

embeddings = OpenAIEmbeddings()
docsearch = Chroma.from_documents(texts, embeddings)

qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type="stuff", retriever=docsearch.as_retriever())
```


```python
query = "What did the president say about Ketanji Brown Jackson"
qa.run(query)
```

<CodeOutputBlock lang="python">

```
    " The president said that she is one of the nation's top legal minds, a former top litigator in private practice, a former federal public defender, and from a family of public school educators and police officers. He also said that she is a consensus builder and has received a broad range of support, from the Fraternal Order of Police to former judges appointed by Democrats and Republicans."
```

</CodeOutputBlock>

## 链类型 （Chain Type）

您可以轻松指定要在 RetrievalQA 链中加载和使用的不同链类型。有关这些类型的更详细的步骤，请参见 [此笔记本](question_answering.html)。

有两种加载不同链类型的方法。首先，您可以在 `from_chain_type` 方法中指定链类型参数。这允许您传递要使用的链类型的名称。例如，在下面的示例中，我们将链类型更改为 `map_reduce`。


```python
qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type="map_reduce", retriever=docsearch.as_retriever())
```


```python
query = "What did the president say about Ketanji Brown Jackson"
qa.run(query)
```

<CodeOutputBlock lang="python">

```
    " The president said that Judge Ketanji Brown Jackson is one of our nation's top legal minds, a former top litigator in private practice and a former federal public defender, from a family of public school educators and police officers, a consensus builder and has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans."
```

</CodeOutputBlock>

上述方法允许您非常简单地更改链类型，但它确实在链类型的参数上提供了大量的灵活性。如果您想要控制这些参数，您可以直接加载链（就像在 [此笔记本](question_answering.html) 中所做的那样），然后将其直接传递给 RetrievalQA 链，使用 `combine_documents_chain` 参数。例如：


```python
from langchain.chains.question_answering import load_qa_chain
qa_chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff")
qa = RetrievalQA(combine_documents_chain=qa_chain, retriever=docsearch.as_retriever())
```


```python
query = "What did the president say about Ketanji Brown Jackson"
qa.run(query)
```

<CodeOutputBlock lang="python">

```
    " The president said that Ketanji Brown Jackson is one of the nation's top legal minds, a former top litigator in private practice, a former federal public defender, and from a family of public school educators and police officers. He also said that she is a consensus builder and has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans."
```

</CodeOutputBlock>

## 自定义提示 （Custom Prompts）

您可以传递自定义提示来进行问题回答。这些提示与您可以传递给 [基本问题回答链](./question_answering.html) 的提示相同


```python
from langchain.prompts import PromptTemplate
prompt_template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.

{context}

Question: {question}
Answer in Italian:"""
PROMPT = PromptTemplate(
    template=prompt_template, input_variables=["context", "question"]
)
```


```python
chain_type_kwargs = {"prompt": PROMPT}
qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type="stuff", retriever=docsearch.as_retriever(), chain_type_kwargs=chain_type_kwargs)
```


```python
query = "What did the president say about Ketanji Brown Jackson"
qa.run(query)
```

<CodeOutputBlock lang="python">

```
    " Il presidente ha detto che Ketanji Brown Jackson è una delle menti legali più importanti del paese, che continuerà l'eccellenza di Justice Breyer e che ha ricevuto un ampio sostegno, da Fraternal Order of Police a ex giudici nominati da democratici e repubblicani."
```

</CodeOutputBlock>
